from interface import Sun, SunBack
from plants import Plant
from zombies import Zombie
from constant import *


class RandomSun:
    def __init__(self, screen: pg.Surface, sun_back: SunBack):
        self.screen = screen
        self.sun_back = sun_back
        self.suns: list[Sun] = []
        self.appear_time = randint(SUN_APPEAR[0], SUN_APPEAR[1]) * 1000
        self.last_time = pg.time.get_ticks()

    def onclick(self, event_pos):
        for sun in self.suns:
            if sun.onclick(event_pos):
                return

    def update(self):
        self.sun_back.update()
        if pg.time.get_ticks() - self.last_time > self.appear_time:
            self.suns.append(Sun(self.screen))
            self.appear_time = randint(SUN_APPEAR[0], SUN_APPEAR[1]) * 1000
            self.last_time = pg.time.get_ticks()
        for sun in self.suns:
            sun.update()
            if sun.coordinate[0] == 0 and sun.coordinate[1] == 0:
                sun.disappear = True
                self.sun_back.sun_count += 25
            elif sun.frames_cycle_index > sun.disappear_cycle_number:
                sun.disappear = True
        self.suns = [sun for sun in self.suns if not sun.disappear]


class Garden:
    def __init__(self, screen: pg.Surface, nature: tuple[Literal['grassland', 'pool', 'roof', 'unsuitable']] | tuple[Literal[0, 1, 2, 3, 4]] = None):
        self.screen = screen
        if nature is None:
            self.nature = ('grassland' for _ in range(5))
        elif isinstance(nature, (list, tuple)):
            if all(struc in (0, 1, 2, 3, 4) for struc in nature):
                self.nature = [GARDEN_STRUCTURE[struc] for struc in nature]
            elif all(struc in GARDEN_STRUCTURE.values() for struc in nature):
                self.nature = nature
            else:
                raise ValueError('structure error')
        self.garden = [[self.Plants(nature) for _ in range(9)] for nature in self.nature]
        self.zombies = self.Zombies()

    class Plants:
        def __init__(self, nature):
            self.nature = nature
            self.plants: list[Plant] = []

        def add(self, new_plant: Plant):
            if new_plant.nature == self.nature:
                self.plants.append(new_plant)

        def update(self):
            for plant in self.plants:
                if plant.current_hp == 0:
                    plant.death = True
                else:
                    plant.update()

    class Zombies:
        def __init__(self):
            self.zombies: list[Zombie] = []

        def add(self, new_zombie: Zombie):
            if isinstance(new_zombie, Zombie):
                self.zombies.append(new_zombie)

        def update(self):
            for zombie in self.zombies:
                if zombie.current_hp == 0:
                    zombie.death = True
                else:
                    zombie.update()

    def update(self):
        for row in self.garden:
            for plants in row:
                plants.update()
        self.zombies.update()






